package com.boy.tool.utils;

/**
 * @author whd
 * @date 2024/06/17 11:20:39
 */
public class NumberUtil {
    /*
    * 将输入的数据转换成汉字
    * 小写*/
    public static String numberToChinese(int number) {
        String[] chineseUnit = {"", "十", "百", "千"};
        String[] chineseDigits = {"", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        StringBuilder result = new StringBuilder();
        int n = number;
        int digitPosition = 0;
        boolean lastNonZero = false;
        while (n > 0) {
            int digit = n % 10;
            if (digit != 0) {
                result.insert(0, chineseDigits[digit] + chineseUnit[digitPosition]);
                lastNonZero = true;
            } else {
                if (lastNonZero) {
                    result.insert(0, "零");
                    lastNonZero = false;
                }
            }
            digitPosition++;
            n /= 10;
        }
        // Handle numbers in the range 10..19 (special case for removing the leading 十)
        if (number >= 10 && number <= 19) {
            result.deleteCharAt(0);
        }

        return result.toString().isEmpty() ? "零" : result.toString();
    }
}
